package StudyCode.summary.mycollection;

import java.util.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class SortMapByComparator {
	public static Map<String, Integer> sortByComparator(
			Map<String, Integer> unsortMap, final boolean order) {

		List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(
				unsortMap.entrySet());

		Collections.sort(list,
				new Comparator<Map.Entry<String, Integer>>() {
					public int compare(Map.Entry<String, Integer> o1,
					                   Map.Entry<String, Integer> o2) {
						if (order) {
							return o1.getValue().compareTo(o2.getValue());
						} else {
							return o2.getValue().compareTo(o1.getValue());

						}
					}
				});

		Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
		for (Map.Entry<String, Integer> entry : list) {
			sortedMap.put(entry.getKey(), entry.getValue());
		}

		return sortedMap;
	}
}
